<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/1/9
 * Time: 14:21
 */
namespace app\service;
use invoice\InvoiceApi as InvoiceApi;
use think\facade\Db;

class invoiceService{
    public function propertyInvoice($property_id){
        $propertyinfo=Db::table("property")
            ->where("id",$property_id)
            ->field("service_id")
            ->find();
        $invoiceinfo=Db::table("service")
            ->where("id",$propertyinfo["service_id"])
            ->find();

        return $invoiceinfo;
    }

    public function getInvoiceConfig($serviceinfo){
        if($serviceinfo["fp_version"]=="v2"){
            if(empty($serviceinfo["fp_app_key_v2"])){
                $res["errCode"]=1;
                $res["errMsg"]="请先配置发票信息";
                return $res;
            }
            //判断access_token是否到期
            $second = strtotime(date('Y-m-d H:i:s',time()))-strtotime($serviceinfo['fp_expires_in_v2']);
            if($second <86400){
                //未过期
                $access_token = $serviceinfo['fp_access_token_v2'];
            }else{
                //过期从新获取
                $result = $this->accessTokenV2($serviceinfo['fp_app_key_v2'],$serviceinfo['fp_app_secret_v2']);
                if($result["errCode"]!=0){
                    return $result;
                }
                //var_dump($result);exit;
                $access_token = $serviceinfo['accessToken'];
            }
            $res["errCode"]=0;
            $res["appKey"] = $serviceinfo["fp_app_key_v2"];
            $res["appSecret"] = $serviceinfo["fp_app_secret_v2"];
            $res["token"] =$access_token;
            $res['method']="nuonuo.OpeMplatform.requestBillingNew";
            $res['queryInvoiceMethod']="nuonuo.OpeMplatform.queryInvoiceResult";
            return $res;

        }else{
            //判断access_token是否到期
            $second = strtotime(date('Y-m-d H:i:s',time()))-strtotime($serviceinfo['fp_expires_in']);
            if($second <86400){
                //未过期
                $access_token = $serviceinfo['fp_access_token'];
            }else{
                //过期从新获取
                $result = $this->accessToken($serviceinfo['fp_app_key'],$serviceinfo['fp_app_secret']);
                if($result["errCode"]!=0){
                    return $result;
                }
                $access_token = $serviceinfo['accessToken'];
            }
            $res["errCode"]=0;
            $res["appKey"] = $serviceinfo["fp_app_key"];
            $res["appSecret"] = $serviceinfo["fp_app_secret"];
            $res["token"] =$access_token;
            $res['method']="nuonuo.electronInvoice.requestBilling";
            $res['queryInvoiceMethod']="nuonuo.electronInvoice.CheckEInvoice";
            return $res;
        }
    }

    public function accessTokenV2($client_id,$client_secret){
        $res = InvoiceApi::getMerchantToken($client_id, $client_secret);
        $tmpInfo = json_decode($res);
        if(property_exists($tmpInfo,'error')){
            $resu["errCode"]=1;
            $resu["errMsg"]="access_token获取失败";
            return $res;
            //var_dump($tmpInfo);die;
            //return $tmpInfo;
           // $this->error($tmpInfo->error.',access_token获取失败');
        }
        $data['fp_access_token_v2'] = $tmpInfo->access_token;//获取access_token
        $data['fp_expires_in_v2'] = date('Y-m-d H:i:s',time());
        $data['id'] = (new billService())->serviceFpAppKeyV2($client_id);
        // 存储access_token
        $result = (new billService())->serviceUpdate($data);
        $resu["errCode"]=0;
        $resu["accessToken"]=$data['fp_access_token_v2'];
        return $resu;
        //return $data['fp_access_token_v2'];
        // $tmpInfo = '{"access_token":"10ffd45208fb4464aa6fee00vthjozss","expires_in":86400}';

    }
    //当access_token超时时，获取access_token
    public function accessToken($client_id,$client_secret){
        $res = InvoiceApi::getMerchantToken($client_id, $client_secret);
        $tmpInfo = json_decode($res);
        if(property_exists($tmpInfo,'error')){
            $resu["errCode"]=1;
            $resu["errMsg"]="access_token获取失败";
            return $res;
            //var_dump($tmpInfo);die;
            //return $tmpInfo;
            //return $tmpInfo;
        }
        $data['fp_access_token'] = $tmpInfo->access_token;//获取access_token
        $data['fp_expires_in'] = date('Y-m-d H:i:s',time());
        $data['id'] = (new billService())->serviceFpAppKey($client_id);
        // 存储access_token
        $result = (new billService())->serviceUpdate($data);
        $resu["errCode"]=0;
        $resu["accessToken"]=$data['fp_access_token'];
        return $resu;
        //return $data['fp_access_token'];
        // $tmpInfo = '{"access_token":"10ffd45208fb4464aa6fee00vthjozss","expires_in":86400}';

    }
}